import moment from "moment";

export function formatTickValue(axis, formatCallback) {
    axis.selectAll(".tick").each(function (d, index, nodes) {
        let nextData = nodes[index + 1]?.__data__ || showEndTime
        let distance = positionComptedFn(nextData) - positionComptedFn(d)
        distance *= 0.5
        d3.select(this)
            .select("text")
            .classed('axis-label', true)
            // .attr("text-anchor", "middle")
            // .attr("transform", `translate(${distance},${(tickSize + fontSize) * 0.5})`)
            .text((d) => {
                // if (distance === 0) return ""
                return formatCallback(d, nextData)
            })

    })
}

export function computeMothMap(begin, end) {
    let mCount = moment(end).startOf("month").diff(moment(begin).startOf("month"), "month");
    const monthMap = {}

    let startMonth = moment(begin).format("YYYY-MM")
    let endMonth = moment(end).format("YYYY-MM")

    for (let i = 0; i <= mCount; i++) {
        let cm = moment(begin).add('month', i).format("YYYY-MM");

        let mStartDay = moment(cm).startOf('month');//当前月开始时间
        let mEndDay = moment(cm).endOf('month');

        if (cm === startMonth && mStartDay.isBefore(moment(begin))) {
            mStartDay = moment(begin)
        }

        if (cm === endMonth && moment(end).isBefore(mEndDay)) {
            mEndDay = moment(end)
        }

        monthMap[cm] = {
            startDay: mStartDay,
            endDay: mEndDay
        }
    }

    return monthMap
}

export function computeDayTicks(everyDay, monthMap) {
    let ticks = new Set()
    let endDay_ = moment()
    for (const month in monthMap) {
        if (Object.hasOwnProperty.call(monthMap, month)) {
            const { startDay, endDay } = monthMap[month];
            endDay_ = endDay
            let s = Number(moment(startDay).format("DD"))
            let e = Number(moment(endDay).format("DD"))

            let lastCache = moment(startDay)
            ticks.add(moment(lastCache).format("YYYY-MM-DD"))

            while (moment(lastCache).isBefore(moment(endDay))) {
                let lastD = Number(moment(lastCache).format("DD"))
                let delta = e - lastD
                if (delta > everyDay) {
                    delta = everyDay
                }
                // if (delta > everyDay * 1.5) {
                //     delta = everyDay

                // }


                ticks.add(moment(lastCache).format("YYYY-MM-DD"))
                lastCache = moment(lastCache).add("day", delta + 1)
            }

            // ticks.add(moment(endDay).format("YYYY-MM-DD"))
        }
    }
    ticks.add(moment(endDay_).format("YYYY-MM-DD"))
    ticks = Array.from(ticks).map(t => moment(t).startOf("day"))

    // let tArr_ = []
    // for (let i = 0; i < ticks.length - 1; i += 1) {
    //     let bIndex = i;
    //     let eIndex = i + 1;

    // }



    let dayTicksDescArr = ticks.map(t => {
        return moment(t).format("YYYY-MM-DD")
    })
    console.log("dd", dayTicksDescArr)
    return ticks

}

export function computeMonthTicks(monthMap) {

    let tickValues = Object.keys(monthMap).map((m) => {
        const { startDay } = monthMap[m]
        return startDay
    })

    return tickValues
}

export function computeYearTicks(monthMap) {
    let tickValuesMap = {}

    Object.keys(monthMap).forEach((m) => {
        const { startDay, endDay } = monthMap[m]

        const year = moment(m).format("YYYY")

        if (!tickValuesMap[year]) {
            tickValuesMap[year] = {
                yearStartDay: startDay,
                yearEndDay: endDay
            }
        }

        const { yearStartDay, yearEndDay } = tickValuesMap[year]
        if (moment(yearEndDay).isBefore(endDay)) {
            tickValuesMap[year].yearEndDay = endDay
        }
    })

    let tickValues = []

    Object.keys(tickValuesMap).forEach(year => {
        const { yearStartDay } = tickValuesMap[year]
        tickValues.push(...[yearStartDay])
    })

    return tickValues

}
